<template>
    <div>
        <Form
                ref="form"
                :form-items="formItems"
                :submit-success="submitSuccess"
        ></Form>
        <MassDoSend :form-data="formData"></MassDoSend>
    </div>

</template>

<script>
    import Form from '../../../../../common-util/src/components/page/Form.vue'
    import MassDoSend from './MassDoSend.vue'
    export default {
        components:{
            Form,
            MassDoSend
        },
        data(){
            return {
           /* {
                "type":"video",
                "media_id":"IhdaAQXuvJtGzwwc0abfXnzeezfO0NgPK6AQYShD8RQYMTtfzbLdBIQkQziv2XJc",
                "created_at":1398848981
            }*/
            submitSuccessData: null,
                formItems:[
                    {
                        field: {
                            name: 'appCode'
                        },
                        element:{
                            type: 'select',
                            select:{
                                url: this.$baseURL.wx + '/wx/mp/config/list',
                                optionProp:{
                                    value: 'appCode',
                                    label: 'appName'
                                }
                            },
                            required: true,
                            label: '公众号',
                        }
                    },
                    {
                        field: {
                            name: 'msgType',
                            value: 'video'
                        }
                    },
                    {
                        field: {
                            name: 'title'
                        },
                        element:{
                            label: '标题',
                            placeholder: '视频的标题',
                            required: true,
                        }
                    },
                    {
                        field: {
                            name: 'description'
                        },
                        element:{
                            label: '描述',
                            placeholder: '视频的描述',
                            required: true,
                        }
                    },
                    {
                        field: {
                            name: 'mediaId'
                        },
                        element:{
                            label: '媒体id',
                            placeholder: '需要在素材管理中获取,永久素材媒体id',
                            required: true,
                        }
                    },
                    {
                        element:{
                            type: 'button',
                            button:[
                                {
                                    action: 'submit',
                                    requestMethod:'post',
                                    url: this.uploadVideoUrl,
                                    label: '获取可群发的素材id'
                                },
                                {
                                    action: 'reset',
                                    label: '重置',
                                    type: 'default'
                                }
                            ]
                        }
                    }
                ],
            }
        },
        methods:{
            submitSuccess(data, form) {
                this.submitSuccessData = data
            },
            uploadVideoUrl(){
                let appCode = this.$refs.form.getFormData().appCode
                if(!appCode){
                    this.$message.error('请选择公众号')
                    return false
                }
                return this.$baseURL.wx + '/wx/mp/mass/' + appCode + '/uploadvideo'
            },
            formData(){
                let r = false
                this.$refs.form.validateAndSubmitForm(()=>{
                    r = true
                })
                if(r == false){
                    this.$message.error('请先获取可群发的素材id')
                    return false
                }
                if (!this.submitSuccessData) {
                    this.$message.error('请先获取可群发的素材id')
                    return false
                }
                let formData =  this.$refs.form.getFormData()
                formData.mediaId = this.submitSuccessData.mediaId
                return formData
            }
        }
    }
</script>

<style scoped>

</style>